//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
    public static void main(String[]args){
        Person person = new Person();
        System.out.println("===父类引用子类的实例===");
        Animal animal1 = new Dog();
        Afraid afraid1 = new Dog();
        System.out.println("\n---Dog实例---");

        System.out.println("通过animal引用调用方法:");
        animal1.eat();

        System.out.println("\n通过Afraid引用调用方法：");
        afraid1.makeSound();
        afraid1.escape();

        System.out.println("\n使用person类的方法");
        person.feed(animal1);
        person.catchAnimal(afraid1);

        Animal animal2 = new Cat();
        Afraid afraid2 = new Cat();

        System.out.println("\n通过Animal引用调用方法：");
        afraid2.makeSound();
        afraid2.escape();

        System.out.println("\n使用Person类的方法：");
        person.feed(animal2);
        person.catchAnimal(afraid2);

        System.out.println("\n===多态演示===");
        Animal[] animals = {new Dog(),new Cat()};
        for (Animal animal : animals){
            System.out.println("\n当前动物：");
            animal.eat();

            if (animal instanceof Afraid){
                Afraid afraidAnimal = (Afraid) animal;
                afraidAnimal.makeSound();
            }
        }
    }
}